from django.shortcuts import render
from .ser import *
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.

class Authorm(APIView):
    def post(self,request):
        ser = Authorser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            return Response({'code':20010})

    def get(self,request):
        se = Author.objects.all()
        ser = Authorser(se,many=True)
        return Response({'code':200,'list':ser.data})


class Authorlogin(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        type1 = int(request.data.get('type1'))
        use = Author.objects.filter(username=username).first()
        if username:
            if use.password == password:
                return Response({'code':200,'userid':use.id,'type':type1})
            else:
                return Response({'code':20010,'meg':'密码错误'})
        else:
            return Response({'code':20011,'meg':'用户不存在'})


from django.core.paginator import Paginator
class Booksm(APIView):
    def post(self,request):
        ser = Bookser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code':20010})

    def get(self,request):
        suoyou  = Books.objects.all()
        p = Paginator(suoyou,3)
        cpage = request.GET.get('p')
        res = p.page(cpage)
        ser = Booksers(res,many=True)
        return Response({'code':200,'list':ser.data,'tpage':p.num_pages})

class Bookmanage(APIView):
    def get(self,request):
        name=request.GET.get('name')
        we = Books.objects.filter(name=name).all()
        ser = Bookser(we,many=True)
        return Response({'code':200,'list':ser.data})



#第二个
class Shopsmanage(APIView):
    def get(self,request):
        se = Shops.objects.all()
        ser = Shopser(se,many=True)
        return Response({'code':200,'list':ser.data})

class Usermanage(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        type2 = int(request.data.get('type2'))
        user = User.objects.filter(username=username).first()
        if username:
            if (user.password==password):
                 if user.type2==type2:
                    return Response({'code':200,'userid':user.id,'type':user.type2})
                 else:
                    return Response({'code':20022,'meg':'等级错误'})
            else:
                return Response({'code':20011,'meg':'密码错误'})
        else:
            return Response({'code':20010,'meg':'用户不存在'})

    def get(self,request):
        se = User.objects.all()
        ser = Userser(se,many=True)
        return Response({'code':200,'list':ser.data})

class Contentmanage(APIView):
    def post(self,request):
        su = User.objects.filter(id = request.data.get('user')).first()
        ser = Contentser(data=request.data)
        if ser.is_valid():
            ser.save()
            if su.type2==1:
                su.jifen+=10
                su.save()
            else:
                su.jifen+=5
                su.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})